

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/zoro.jpg">
  <link rel="icon" href="/img/zoro.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#efb9b9">
  <meta name="description" content="">
  <meta name="author" content="AgoniLay">
  <meta name="keywords" content="">
  
  <title>Express笔记 - AgoniLay的博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/arta.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="/css/scroller.css">
<link rel="stylesheet" href="/css/bynode.css">
<link rel="stylesheet" href="/css/mouse.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"agonilay.github.io","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":150,"cursorChar":"❤","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading2.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>AgoniLay</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-bookmark"></i>
                索引
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" href="/archives/">
                    <i class="iconfont icon-archive-fill"></i>
                    归档
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="/categories/">
                    <i class="iconfont icon-category-fill"></i>
                    分类
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="/tags/">
                    <i class="iconfont icon-tags-fill"></i>
                    标签
                  </a>
                
              </div>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
          
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/zoro2.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h3" id="subtitle" title="Express笔记">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      AgoniLay
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-07-29 21:55" pubdate>
        2021年7月29日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      113
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
			
			
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Express笔记</h1>
            
            <div class="markdown-body">
              <h2 id="一、Express-简介"><a href="#一、Express-简介" class="headerlink" title="一、Express 简介"></a>一、Express 简介</h2><h4 id="是什么"><a href="#是什么" class="headerlink" title="是什么"></a>是什么</h4><p>基于 nodejs 开发的一个框架</p>
<h4 id="好处"><a href="#好处" class="headerlink" title="好处"></a>好处</h4><p>加快项目开发，便于团队协作</p>
<h2 id="二、使用（初体验）"><a href="#二、使用（初体验）" class="headerlink" title="二、使用（初体验）"></a>二、使用（初体验）</h2><ol>
<li><p>创建开发目录，初始化</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm init -y<br></code></pre></div></td></tr></table></figure></li>
<li><p>安装 express</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm install express<br></code></pre></div></td></tr></table></figure></li>
<li><p>使用</p>
<p>app.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 1. 引入模块</span><br><span class="hljs-keyword">var</span> experss = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">// 2. 创建 web服务器</span><br><span class="hljs-keyword">var</span> app = experss()<br><span class="hljs-comment">// 3. 路由</span><br>app.get(<span class="hljs-string">&#x27;/&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>&#123;<br>    <span class="hljs-comment">// end()    响应字符串（乱码）</span><br>    <span class="hljs-comment">// send()   响应字符串（自动识别）</span><br>    <span class="hljs-comment">// render() 响应字符串（自动识别，只能获取指定文件字符串并响应，注：必须配合模板引擎使用）</span><br><br>    <span class="hljs-comment">// res.end(&quot;哥哥来抓我呀，&lt;a href=&#x27;http://nn.com&#x27;&gt;点击进入我的世界&lt;/a&gt;&quot;)</span><br>    res.send(<span class="hljs-string">&quot;哥哥来抓我呀，&lt;a href=&#x27;http://nn.com&#x27;&gt;点击进入我的世界&lt;/a&gt;&quot;</span>)<br>&#125;)<br><span class="hljs-comment">// 4. 启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;启动成功，访问：http://localhost:8080&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h2 id="三、配置模板引擎（art）"><a href="#三、配置模板引擎（art）" class="headerlink" title="三、配置模板引擎（art）"></a>三、配置模板引擎（art）</h2><ul>
<li>默认通过公 end 或 send 渲染，无法加载视图，所以得自己配置默认模板引擎官方以前推荐用 jade 现在升级为 pug 也有用 ejs 个人推荐 art 模板引擎</li>
<li>文档：<a target="_blank" rel="noopener" href="http://aui.github.io/art-template/zh-cn/express/">http://aui.github.io/art-template/zh-cn/express/</a></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js">app.engine(<span class="hljs-string">&#x27;html&#x27;</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express-art-template&#x27;</span>))<br></code></pre></div></td></tr></table></figure>

<h4 id="安装使用"><a href="#安装使用" class="headerlink" title="安装使用"></a>安装使用</h4><p>安装</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm install art-template<br>npm install express-art-template<br></code></pre></div></td></tr></table></figure>

<p>创建 views 目录</p>
<p>test1.html</p>
<figure class="highlight html"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#123;&#123;username&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#123;&#123;age&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>            &#123;&#123;if age &gt; 18&#125;&#125;<br>            妹妹哥哥来了<br>            &#123;&#123;else&#125;&#125;<br>            你走吧<br>            &#123;&#123;/if&#125;&#125;<br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>            &#123;&#123;each orders as order index&#125;&#125;<br>            &#123;&#123;index&#125;&#125; &#123;&#123;order.title&#125;&#125;<br>            &#123;&#123;/each&#125;&#125;<br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></div></td></tr></table></figure>

<p>app2.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 1. 引入模块</span><br><span class="hljs-keyword">var</span> experss = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">// 2. 创建 web服务器</span><br><span class="hljs-keyword">var</span> app = experss()<br><span class="hljs-comment">// 3. 配置</span><br><span class="hljs-comment">// 声明所使用的模板引擎（ps. 使用render方法必须）</span><br>app.engine(<span class="hljs-string">&#x27;html&#x27;</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express-art-template&#x27;</span>))<br><span class="hljs-comment">// 4. 路由</span><br>app.get(<span class="hljs-string">&#x27;/&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>&#123;<br>    <span class="hljs-comment">// end()    响应字符串（乱码）</span><br>    <span class="hljs-comment">// send()   响应字符串（自动识别）</span><br>    <span class="hljs-comment">// render() 响应字符串（自动识别，只能获取指定文件字符串并响应，注：必须配合模板引擎使用）</span><br><br>    <span class="hljs-comment">// res.end(&quot;哥哥来抓我呀，&lt;a href=&#x27;http://nn.com&#x27;&gt;点击进入我的世界&lt;/a&gt;&quot;)</span><br>    <span class="hljs-comment">// res.send(&quot;哥哥来抓我呀，&lt;a href=&#x27;http://nn.com&#x27;&gt;点击进入我的世界&lt;/a&gt;&quot;);</span><br>    res.render(<span class="hljs-string">&#x27;test1.html&#x27;</span>, &#123;<br>        <span class="hljs-attr">username</span>: <span class="hljs-string">&#x27;张艺兴&#x27;</span>,<br>        <span class="hljs-attr">age</span>: <span class="hljs-number">5</span>,<br>        <span class="hljs-attr">orders</span>: [<br>            &#123;<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;标题1&#x27;</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">30</span>&#125;,<br>            &#123;<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;标题2&#x27;</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">33</span>&#125;,<br>            &#123;<span class="hljs-attr">id</span>:<span class="hljs-number">3</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;标题3&#x27;</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">12</span>&#125;,<br>        ], <br>    &#125;)<br>&#125;)<br><span class="hljs-comment">// 5. 启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;启动成功，访问：http://localhost:8080&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h2 id="四、路由"><a href="#四、路由" class="headerlink" title="四、路由"></a>四、路由</h2><h5 id="扩展：HTTP请求类型"><a href="#扩展：HTTP请求类型" class="headerlink" title="扩展：HTTP请求类型"></a>扩展：HTTP请求类型</h5><div class="hljs code-wrapper"><pre><code>OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACEa
</code></pre></div>
<h4 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h4><ul>
<li>什么是路由：就是网址</li>
<li>什么是路由参数：就是网址参数</li>
<li>在 PHP 中作用：声明交给哪个控制器的哪个方法处理</li>
<li>在 NodeJS 中作用：交给指定匿名函数处理</li>
</ul>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p><em><strong>普通语法</strong></em>：app.HTTP请求类型（请求路径，回调函数）</p>
<p>发送<em><strong>GET</strong></em>请求：app.<em><strong>get</strong></em>（请求路径，回调函数）</p>
<p>发送<em><strong>POST</strong></em>请求：app.<em><strong>post</strong></em>（请求路径，回调函数）</p>
<p>发送<em><strong>任意</strong></em>请求：app.<em><strong>all</strong></em>（请求路径，回调函数）</p>
<p><em><strong>特殊语法：</strong></em>app.<em><strong>use</strong></em>（请求路径，回调函数）</p>
<p>区别1：use匹配任意类型请求</p>
<p>区别2：<em><strong>use非完全匹配</strong></em>（ps. 只需要<em><strong>url前面匹配</strong></em>请求路径即可匹配）</p>
<p><em><strong>路由参数：</strong></em>app.<em><strong>HTTP请求类型</strong></em>（请求路径/:参数1/…/:参数n，回调函数）</p>
<h4 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h4><p>app3.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">//1.引入express框架模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">//2.创建框架核心app对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">//3.路由</span><br>app.get(<span class="hljs-string">&#x27;/&#x27;</span>,  <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>&#123;<br><br>	<span class="hljs-comment">//注：修改method=get 或者 method=post查看结果</span><br>	<span class="hljs-keyword">var</span> formHtml = <span class="hljs-string">`</span><br><span class="hljs-string">		&lt;form action=&quot;/test&quot; method=&quot;post&quot;&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;text&quot; name=&quot;uname&quot; /&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;text&quot; name=&quot;age&quot; /&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;submit&quot;  /&gt;</span><br><span class="hljs-string">		&lt;/form&gt;</span><br><span class="hljs-string">	`</span><br>	res.send(formHtml)<br>&#125;)<br><br>app.get(<span class="hljs-string">&#x27;/test&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>	res.send(<span class="hljs-string">&#x27;this is get submit&#x27;</span>)<br>&#125;)<br><br>app.post(<span class="hljs-string">&#x27;/test&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>	res.send(<span class="hljs-string">&#x27;this is post submit&#x27;</span>)<br>&#125;)<br><br><span class="hljs-comment">//4.启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;<br>	<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Running...&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h4 id="特殊语法练习"><a href="#特殊语法练习" class="headerlink" title="特殊语法练习"></a>特殊语法练习</h4><p>app4.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">//1.引入express框架模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">//2.创建框架核心app对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">//3.路由</span><br>app.get(<span class="hljs-string">&#x27;/&#x27;</span>,  <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>) </span>&#123;<br><br>	<span class="hljs-keyword">var</span> formHtml = <span class="hljs-string">`</span><br><span class="hljs-string">		&lt;form action=&quot;/test/a/b/c&quot; method=&quot;post&quot;&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;text&quot; name=&quot;uname&quot; /&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;text&quot; name=&quot;age&quot; /&gt;</span><br><span class="hljs-string">			&lt;input type=&quot;submit&quot;  /&gt;</span><br><span class="hljs-string">		&lt;/form&gt;</span><br><span class="hljs-string">	`</span><br>	res.send(formHtml)<br>&#125;)<br><br>app.use(<span class="hljs-string">&#x27;/test&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>	res.send(<span class="hljs-string">&#x27;this is /test&#x27;</span>)<br>&#125;)<br><br><span class="hljs-comment">//4.启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;<br>	<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Running...&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h4 id="路由参数练习"><a href="#路由参数练习" class="headerlink" title="路由参数练习"></a>路由参数练习</h4><p>app5.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">//1.引入express框架模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">//2.创建框架核心app对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">//3.路由</span><br>app.get(<span class="hljs-string">&#x27;/stu/:name/:age&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>	<span class="hljs-built_in">console</span>.log(req.params);<br>	res.send(<span class="hljs-string">&#x27;匹配成功，快去看控制台有没有获取数据..&#x27;</span>)<br>&#125;)<br><span class="hljs-comment">//4.启动服务 </span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;<br>	<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Running...&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h2 id="五、利用-Express-托管静态文件"><a href="#五、利用-Express-托管静态文件" class="headerlink" title="五、利用 Express 托管静态文件"></a>五、利用 Express 托管静态文件</h2><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js">app.use(<span class="hljs-string">&#x27;/public&#x27;</span>, express.static(<span class="hljs-string">&#x27;public&#x27;</span>))<br></code></pre></div></td></tr></table></figure>

<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">//1.引入express框架模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs&#x27;</span>)<br><span class="hljs-comment">//2.创建框架核心app对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">//3.路由</span><br><span class="hljs-comment">// 响应指定静态资源</span><br>app.get(<span class="hljs-string">&#x27;/public/css/a.css&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>    fs.readFile(<span class="hljs-string">&#x27;./public/css/a.css&#x27;</span>, <span class="hljs-string">&#x27;utf8&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, data</span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (err) res.send(err);<br>        res.send(data)<br>    &#125;)<br>&#125;)<br><span class="hljs-comment">// 响应多个静态资源</span><br><span class="hljs-comment">// app.use(&#x27;/public&#x27;, function(req, res)&#123;</span><br><span class="hljs-comment">// 	fs.readFile(&#x27;./public/&#x27; + req.url , &#x27;utf8&#x27;, function (err, data) &#123;</span><br><span class="hljs-comment">// 		if (err) res.send(err);</span><br><span class="hljs-comment">// 		res.send(data)</span><br><span class="hljs-comment">// 	&#125;)</span><br><span class="hljs-comment">// &#125;)</span><br><span class="hljs-comment">// 在express中</span><br><span class="hljs-comment">// 作用：允许指定目录下的文件被外访问</span><br><span class="hljs-comment">// 语法：express.static(&#x27;目录名&#x27;)   </span><br><span class="hljs-comment">// 返回：一个函数，可理解为读取URL需要读取的文件</span><br>app.use(<span class="hljs-string">&#x27;/public&#x27;</span>, express.static(<span class="hljs-string">&#x27;public&#x27;</span>))<br><span class="hljs-comment">//4.启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Running...&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h2 id="六、案例"><a href="#六、案例" class="headerlink" title="六、案例"></a>六、案例</h2><h3 id="NodeJS-each循环"><a href="#NodeJS-each循环" class="headerlink" title="NodeJS each循环"></a>NodeJS each循环</h3><figure class="highlight html"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- node.js each循环 --&gt;</span><br>&#123;&#123;each msgs as msg index&#125;&#125;<br><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span>&gt;</span><br>    &#123;&#123;msg.name&#125;&#125;说:&#123;&#123;msg.content&#125;&#125;<br>    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;pull-right&quot;</span>&gt;</span>&#123;&#123;msg.create_at&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>&#123;&#123;/each&#125;&#125;<br></code></pre></div></td></tr></table></figure>

<h3 id="切换-NodeJS-版本"><a href="#切换-NodeJS-版本" class="headerlink" title="切换 NodeJS 版本"></a>切换 NodeJS 版本</h3><h4 id="nvm"><a href="#nvm" class="headerlink" title="nvm"></a>nvm</h4><p>通过 nvm 工具可以实现 node.js 任意版本切换</p>
<h5 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h5><p><a target="_blank" rel="noopener" href="http://github.com/coreybutler/nvm-windows/releases">http://github.com/coreybutler/nvm-windows/releases</a></p>
<h5 id="命令"><a href="#命令" class="headerlink" title="命令"></a>命令</h5><figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">nvm version<br>nvm install   latest<br>nvm install   版本号<br>nvm uninstall 版本号<br>nvm <span class="hljs-keyword">list</span><br>nvm <span class="hljs-keyword">use</span>	      版本号<br></code></pre></div></td></tr></table></figure>

<h2 id="七、优化"><a href="#七、优化" class="headerlink" title="七、优化"></a>七、优化</h2><p>通过应用生成器工具 <code>express-generator</code> 可以快速创建一个应用的骨架。</p>
<p>网站：<a target="_blank" rel="noopener" href="http://www.expressjs.com.cn/">http://www.expressjs.com.cn</a></p>
<h3 id="7-1-路由优化（理解）"><a href="#7-1-路由优化（理解）" class="headerlink" title="7.1 路由优化（理解）"></a>7.1 路由优化（理解）</h3><h4 id="方法1：（不推荐）"><a href="#方法1：（不推荐）" class="headerlink" title="方法1：（不推荐）"></a>方法1：（不推荐）</h4><p>创建 routes 目录，在中创建 stu.js 文件，专门负责路由管理</p>
<p>stu.js：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">app, fs</span>) </span>&#123;<br>    <span class="hljs-comment">// 路由部分</span><br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>app.js：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 4. 路由</span><br><span class="hljs-comment">// var indexRouter = require(&#x27;./routes/index&#x27;);</span><br><span class="hljs-comment">// var orderRouter = require(&#x27;./routes/order&#x27;);</span><br><span class="hljs-keyword">var</span> stuRouter = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;./routes/stu&#x27;</span>);<br><span class="hljs-comment">// indexRouter(app, fs)</span><br><span class="hljs-comment">// orderRouter(app, fs)</span><br>stuRouter(app, fs)<br></code></pre></div></td></tr></table></figure>

<h4 id="方法2："><a href="#方法2：" class="headerlink" title="方法2："></a>方法2：</h4><ol>
<li><p>修改 app.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// var indexRouter = require(&#x27;./routes/index&#x27;);</span><br><span class="hljs-comment">// var orderRouter = require(&#x27;./routes/order&#x27;);</span><br>   <span class="hljs-keyword">var</span> stuRouter = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;./routes/stu&#x27;</span>);<br><span class="hljs-comment">// app.use(&#x27;/&#x27;,	     indexRouter);</span><br><span class="hljs-comment">// app.use(&#x27;/order&#x27;, orderRouter);</span><br>   app.use(<span class="hljs-string">&#x27;/stu&#x27;</span>, stuRouter);<br></code></pre></div></td></tr></table></figure></li>
<li><p>stu.js</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs&#x27;</span>)<br><span class="hljs-keyword">var</span> router = express.Router()<br><br><span class="hljs-comment">// 路由部分</span><br><br><span class="hljs-built_in">module</span>.exports = router<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="7-2-数据操作优化（理解）"><a href="#7-2-数据操作优化（理解）" class="headerlink" title="7.2 数据操作优化（理解）"></a>7.2 数据操作优化（理解）</h3><p>创建 models 文件夹，新建 stu.js </p>
<p>在其中创建模型</p>
<p>然后再路由当中调用，避免冗余，便于后期扩展开发</p>
<h2 id="八、Express-框架"><a href="#八、Express-框架" class="headerlink" title="八、Express 框架"></a>八、Express 框架</h2><h3 id="8-1-中间件"><a href="#8-1-中间件" class="headerlink" title="8.1 中间件"></a>8.1 中间件</h3><h5 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h5><p>HTTP请求中的过滤层（HTTP请求交给方法处理前，先交给另一个方法过滤）</p>
<ul>
<li>PHP 中：BaseController</li>
<li>NodeJS 中：app.use()</li>
</ul>
<h5 id="应用场景"><a href="#应用场景" class="headerlink" title="应用场景"></a>应用场景</h5><p>判断登录状态、检测RBAC权限、错误捕捉等</p>
<h3 id="8-3-路由中间件"><a href="#8-3-路由中间件" class="headerlink" title="8.3 路由中间件"></a>8.3 路由中间件</h3><ol>
<li><p>创建 day3 目录</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php"><span class="hljs-comment">// 初始化</span><br>npm init -y<br><span class="hljs-comment">// 安装 express 以及 art</span><br>npm install express art-template express-art-template<br></code></pre></div></td></tr></table></figure></li>
<li><p>app.js 中：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 1. 引入 express 模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">// 2. 创建 app 对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">// 3. 配置</span><br><span class="hljs-comment">// 4. 路由</span><br><br><span class="hljs-comment">// 中间件</span><br>app.use(<span class="hljs-string">&#x27;/&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>);<br>    next(); <span class="hljs-comment">// 继续向后访问</span><br>&#125;)<br><br>app.get(<span class="hljs-string">&#x27;/stu&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;stu list&#x27;</span>);<br>    res.send(<span class="hljs-string">&#x27;stu list&#x27;</span>) <span class="hljs-comment">// end send render</span><br>&#125;)<br>app.get(<span class="hljs-string">&#x27;/stu/create&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;stu add&#x27;</span>);<br>    res.send(<span class="hljs-string">&#x27;stu add&#x27;</span>) <span class="hljs-comment">// end send render</span><br>&#125;)<br><span class="hljs-comment">// 5. 启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;启动成功...&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<p>中间件匹配成功后，不会继续往下寻找，需要使用 <code>next()</code>继续访问</p>
<p>使用<code>next()</code>函数需要添加 next 形参</p>
<p><code>function (req, res, next) &#123;&#125;</code></p>
</li>
</ol>
<h3 id="8-4-错误处理"><a href="#8-4-错误处理" class="headerlink" title="8.4 错误处理"></a>8.4 错误处理</h3><h5 id="中间件灵活使用"><a href="#中间件灵活使用" class="headerlink" title="中间件灵活使用"></a>中间件灵活使用</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js">app.get(<span class="hljs-string">&#x27;/stu&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>&#123;<br>    fs.readFile(<span class="hljs-string">&#x27;./aaaa.txt&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, data</span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (err) next(err) <span class="hljs-comment">// next如果不传递参数 只匹配下一个URL</span><br>        				   <span class="hljs-comment">// next传递err 则匹配第一个含有err的中间件</span><br>        res.send(data) <span class="hljs-comment">// end send render</span><br>    &#125;)<br>&#125;)<br><br><span class="hljs-comment">// 中间件灵活使用（404）</span><br>app.use(<span class="hljs-string">&#x27;/&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>&#123;<br>    res.send(<span class="hljs-string">&#x27;404 Page&#x27;</span>)<br>&#125;)<br><span class="hljs-comment">// 错误统一处理</span><br><span class="hljs-comment">// 脚下留心：（形参可以不写，但顺序不能改变）</span><br><span class="hljs-comment">// &#x27;/&#x27; 可以省略不写，等价</span><br><span class="hljs-comment">// app.use(function (err, req, res, next) &#123;</span><br>app.use(<span class="hljs-string">&#x27;/&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, req, res, next</span>) </span>&#123;<br>    res.send(<span class="hljs-string">&#x27;网络异常... 请稍后重试&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<ul>
<li>形参可以不写，但顺序不能改变</li>
<li>‘/‘ 可以省略不写，与写上等价</li>
<li>next 如果不传递参数 只匹配下一个 URL；next 传递 err  则匹配第一个含有 err 的中间件</li>
</ul>
<h2 id="九、会话技术"><a href="#九、会话技术" class="headerlink" title="九、会话技术"></a>九、会话技术</h2><h3 id="9-1-cookie"><a href="#9-1-cookie" class="headerlink" title="9.1 cookie"></a>9.1 cookie</h3><h5 id="手册"><a href="#手册" class="headerlink" title="手册"></a>手册</h5><p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/cookie-parser">https://www.npmjs.com/package/cookie-parser</a></p>
<h5 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h5><ol>
<li><p>安装cookie模块，语法：<code>npm install cookie-parser</code></p>
</li>
<li><p>引入cookie模块，语法：<code>var cookieParser = require(&#39;cookie-parser&#39;)</code></p>
</li>
<li><p>初始化cookie，语法：<code>app.use(cookieParser())</code></p>
<div class="hljs code-wrapper"><pre><code>                   相当于：`app.user(&#39;/&#39;, cookieParser())`
</code></pre></div>
<p><strong>作用</strong>：http传入的cookie进行解析后赋值给req.cookies</p>
<p><strong>其次</strong>：cookieParser加参数，并且设置是signed=true则加密存储（推荐）</p>
</li>
</ol>
<h6 id="设置"><a href="#设置" class="headerlink" title="设置"></a>设置</h6><p><code>res.cookie(键, 值, &#123;signed: 是否加密true-是,false-否默认,maxAge: 时间/毫秒&#125;)</code></p>
<p>获取（未加密）：<code>req.cookies</code></p>
<p>获取（  加密）：<code>req.signedCookies</code></p>
<p>清除：<code>res.clearCookie(键)</code></p>
<h5 id="代码测试"><a href="#代码测试" class="headerlink" title="代码测试"></a>代码测试</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 需求：设置cookie数据a=1永久，b=2（1分钟）c=3（加密）d=4（删除）</span><br><span class="hljs-comment">//1.引入express框架模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;cookie-parser&#x27;</span>)<br><span class="hljs-comment">//2.创建框架核心app对象</span><br><span class="hljs-keyword">var</span> app = express() <br><span class="hljs-comment">//3.配置</span><br><span class="hljs-comment">//http传入的cookie进行解析后赋值给req.cookies</span><br>app.use(cookieParser(<span class="hljs-string">&#x27;itcast&#x27;</span>))		<br><span class="hljs-comment">//4.路由</span><br>app.get(<span class="hljs-string">&#x27;/set&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>    <span class="hljs-comment">//res.cookie(键, 值, &#123;signed: 是否加密true-是,false-否默认,maxAge: 时间/毫秒&#125;)</span><br>    res.cookie(<span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-number">1</span>)<br>    res.cookie(<span class="hljs-string">&#x27;b&#x27;</span>, <span class="hljs-number">2</span>, &#123;<span class="hljs-attr">maxAge</span>: <span class="hljs-number">60</span>*<span class="hljs-number">1000</span>&#125;)<br>    res.cookie(<span class="hljs-string">&#x27;c&#x27;</span>, <span class="hljs-number">3</span>, &#123;<span class="hljs-attr">signed</span>: <span class="hljs-literal">true</span>&#125;)<br>    res.cookie(<span class="hljs-string">&#x27;d&#x27;</span>, <span class="hljs-number">4</span>)<br>    res.send(<span class="hljs-string">&#x27;设置成功&#x27;</span>)<br>&#125;)<br>app.get(<span class="hljs-string">&#x27;/get&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;未加密&#x27;</span>)<br>    <span class="hljs-built_in">console</span>.log(req.cookies)<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;加密&#x27;</span>)<br>    <span class="hljs-built_in">console</span>.log(req.signedCookies)<br>    res.send(<span class="hljs-string">&#x27;打开控制台查看&#x27;</span>)<br>&#125;)<br>app.get(<span class="hljs-string">&#x27;/del&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>)</span>&#123;<br>    <span class="hljs-comment">//res.clearCookie(键)</span><br>    res.clearCookie(<span class="hljs-string">&#x27;d&#x27;</span>)<br>    res.send(<span class="hljs-string">&#x27;删除成功，重新访问/get验证&#x27;</span>)<br>&#125;)<br><span class="hljs-comment">//5.启动服务 </span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;Running...&#x27;</span>)<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h3 id="9-2-session"><a href="#9-2-session" class="headerlink" title="9.2 session"></a>9.2 session</h3><h5 id="手册-1"><a href="#手册-1" class="headerlink" title="手册"></a>手册</h5><p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/express-session">https://www.npmjs.com/package/express-session</a></p>
<h5 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h5><ol>
<li>安装 session 模块，语法：<code>npm install express-session</code></li>
<li>引入 session 模块，语法：<code>var session = require(&#39;express-session&#39;)</code></li>
<li>初始化 session，语法：</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js">app.use(session (&#123;<br>    <span class="hljs-attr">secret</span>: <span class="hljs-string">&#x27;itcast&#x27;</span>,	 	   <span class="hljs-comment">// 加密存储</span><br>    <span class="hljs-attr">resave</span>: <span class="hljs-literal">false</span>,		 	   <span class="hljs-comment">// 客户端并行请求是否覆盖:true-是,false-否</span><br>    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>    <span class="hljs-comment">// 初始化session存储</span><br>&#125;))<br></code></pre></div></td></tr></table></figure>

<p>设置：<code>req.session.键 = 值</code></p>
<p>获取：<code>req.session.键</code></p>
<p>清除：<code>req.session.键 = null</code></p>
<h5 id="代码测试-1"><a href="#代码测试-1" class="headerlink" title="代码测试"></a>代码测试</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 1. 引入 express 模块</span><br><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-keyword">var</span> session = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express-session&#x27;</span>)<br><span class="hljs-comment">// 2. 创建 app 对象</span><br><span class="hljs-keyword">var</span> app = express()<br><span class="hljs-comment">// 3. 配置</span><br>app.use(session(&#123;<br>    <span class="hljs-attr">secret</span>: <span class="hljs-string">&#x27;itcast&#x27;</span>,	  	  	<span class="hljs-comment">//加密存储</span><br>    <span class="hljs-attr">resave</span>: <span class="hljs-literal">false</span>,		  	    <span class="hljs-comment">//客户端并行请求是否覆盖:true-是,false-否</span><br>    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>   <span class="hljs-comment">//初始化session存储</span><br>&#125;))<br><span class="hljs-comment">// 4. 路由（需  求：设置session数据a=1，b=2，c=3 最后删除c）</span><br>app.get(<span class="hljs-string">&#x27;/set&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>&#123;<br>    req.session.a = <span class="hljs-number">1</span><br>    req.session.b = <span class="hljs-number">2</span><br>    req.session.c = <span class="hljs-number">3</span><br>    res.send(<span class="hljs-string">&#x27;设置成功，通过/get获取&#x27;</span>)<br>&#125;)<br>app.get(<span class="hljs-string">&#x27;/get&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(req.session.a);<br>    <span class="hljs-built_in">console</span>.log(req.session.b);<br>    <span class="hljs-built_in">console</span>.log(req.session.c);<br>    res.send(<span class="hljs-string">&#x27;获取成功，请通过控制台查看&#x27;</span>)<br>&#125;)<br>app.get(<span class="hljs-string">&#x27;/del&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>&#123;<br>    req.session.c = <span class="hljs-literal">null</span><br>    res.send(<span class="hljs-string">&#x27;删除成功，请通过/get重新查看&#x27;</span>)<br>&#125;)<br><span class="hljs-comment">// 5. 启动服务</span><br>app.listen(<span class="hljs-number">8080</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;启动成功...&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<p>cookie 在客户端；session 在服务端</p>
<h2 id="十、总结"><a href="#十、总结" class="headerlink" title="十、总结"></a>十、总结</h2><h4 id="如何展示静态页面"><a href="#如何展示静态页面" class="headerlink" title="如何展示静态页面"></a>如何展示静态页面</h4><h6 id="在-PHP-中"><a href="#在-PHP-中" class="headerlink" title="在 PHP 中"></a>在 PHP 中</h6><ol>
<li>确定路由</li>
<li>根据路由创建控制器方法 -&gt; 加载视图 display 或 view 函数</li>
<li>创建视图 -&gt; 修改静态资源路径</li>
</ol>
<h6 id="在-NodeJS-中"><a href="#在-NodeJS-中" class="headerlink" title="在 NodeJS 中"></a>在 NodeJS 中</h6><ol>
<li>定义路由 -&gt; 通过render加载视图传递数据</li>
<li>创建视图 -&gt; 修改静态资源路径</li>
</ol>
<h4 id="如何数据处理"><a href="#如何数据处理" class="headerlink" title="如何数据处理"></a>如何数据处理</h4><ol>
<li>修改表单属性值</li>
<li>接受数据</li>
<li>逻辑数据</li>
<li>数据库操作</li>
<li>判断跳转</li>
</ol>
<h4 id="如何显示列表"><a href="#如何显示列表" class="headerlink" title="如何显示列表"></a>如何显示列表</h4><ol>
<li>控制器方法获取数据 -&gt; 传递给视图</li>
<li>调用模板标签循环显示</li>
</ol>
<h4 id="如何实现-增删改"><a href="#如何实现-增删改" class="headerlink" title="如何实现 增删改"></a>如何实现 增删改</h4><ol>
<li>修改静态页面（前：修改表单 action name    后：修改 a 标签 id）</li>
<li>数据处理（1-接受数据，2-数据过滤，3-入库，4-判断跳转）</li>
</ol>
<h3 id="express-模板框架生成器"><a href="#express-模板框架生成器" class="headerlink" title="express 模板框架生成器"></a>express 模板框架生成器</h3><p>全局安装</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm install -g express-<span class="hljs-built_in">generator</span><br></code></pre></div></td></tr></table></figure>

<p>在想要生成项目的目录下终端输入</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">express 项目名<br></code></pre></div></td></tr></table></figure>

<p>即可自动生成</p>
<p>然后</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">cd 项目名<br>npm install<br>npm install art-template express-art-template<br></code></pre></div></td></tr></table></figure>

<p>激活默认模板引擎</p>
<p>替换</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// app.set(&#x27;views&#x27;, path.join(__dirname, &#x27;views&#x27;));</span><br><span class="hljs-comment">// app.set(&#x27;view engine&#x27;, &#x27;jade&#x27;);</span><br></code></pre></div></td></tr></table></figure>

<p>为</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js">app.use(<span class="hljs-string">&#x27;/public&#x27;</span>, express.static(<span class="hljs-string">&#x27;public&#x27;</span>))<br>app.set(<span class="hljs-string">&#x27;views&#x27;</span>, path.join(__dirname, <span class="hljs-string">&#x27;views&#x27;</span>));<br>app.engine(<span class="hljs-string">&#x27;html&#x27;</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express-art-template&#x27;</span>));<br>app.set(<span class="hljs-string">&#x27;view engine&#x27;</span>, <span class="hljs-string">&#x27;html&#x27;</span>);<br></code></pre></div></td></tr></table></figure>

<p>即可进行后续开发</p>
<h1 id="ES6-扩展"><a href="#ES6-扩展" class="headerlink" title="ES6 扩展"></a>ES6 扩展</h1><h2 id="数组扩展新语法"><a href="#数组扩展新语法" class="headerlink" title="数组扩展新语法"></a>数组扩展新语法</h2><h4 id="find-搜索"><a href="#find-搜索" class="headerlink" title="find() 搜索"></a><code>find()</code> 搜索</h4><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// var arr = [</span><br><span class="hljs-comment">// 	&#123;name:&#x27;test1&#x27;, age: 1&#125;,</span><br><span class="hljs-comment">// 	&#123;name:&#x27;test2&#x27;, age: 2&#125;,</span><br><span class="hljs-comment">// 	&#123;name:&#x27;test3&#x27;, age: 3&#125;</span><br><span class="hljs-comment">// ];</span><br><br><span class="hljs-comment">// //需求：获取name=test3的元素下标</span><br><span class="hljs-comment">// var index = arr.findIndex(function(item)&#123;</span><br><span class="hljs-comment">// 	return item.name == &#x27;test3&#x27;</span><br><span class="hljs-comment">// &#125;)</span><br><span class="hljs-comment">// console.log(index)</span><br><span class="hljs-comment">// //需求：获取name=test3的元素</span><br><span class="hljs-comment">// var obj = arr.find(function(item)&#123;</span><br><span class="hljs-comment">// 	return item.name == &#x27;test3&#x27;</span><br><span class="hljs-comment">// &#125;)</span><br><span class="hljs-comment">// console.log(obj)</span><br><span class="hljs-comment">// console.log(&#x27;------------&#x27;)</span><br><br><span class="hljs-comment">//原理</span><br><span class="hljs-comment">//JS面向对象：封装 、 继承、 多态</span><br><span class="hljs-comment">//JS封装：this 私有的  var 公开的 （类控制成员只保留部分的成员和方法供外部使用 通过修饰符 public private protected）</span><br><span class="hljs-comment">//JS继承：prototype  		      （extend）</span><br><br><span class="hljs-comment">//var xx = [1,2] 相当于 var xx  = new Array(1,2)   </span><br><span class="hljs-comment">//var xx = &#x27;aaa&#x27; 相当于 var xx  = new String(&#x27;aaa&#x27;)</span><br><span class="hljs-comment">//Array是系统类</span><br><span class="hljs-comment">//现在通过原先继承，给他增加一个myFind方法</span><br><span class="hljs-built_in">Array</span>.prototype.myFind = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">conditionFunc</span>) </span>&#123;<br>    <span class="hljs-comment">//conditionFunc == function(item) &#123; return item.name == &#x27;test3&#x27; &#125;</span><br>    <span class="hljs-comment">//this指调用当前方法的对象（注：下述代码 arr调用myFind）</span><br>    <span class="hljs-comment">//所以this指arr</span><br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) &#123;<br>        <span class="hljs-comment">//判断条件是否成立，成立就返回（注：this指数组，this[i] 数组中的元素）</span><br>        <span class="hljs-keyword">if</span> (conditionFunc(<span class="hljs-built_in">this</span>[i])) &#123;<br>            <span class="hljs-comment">//return this[i]  //相当于 es6中的find</span><br>            <span class="hljs-keyword">return</span> i  		<span class="hljs-comment">//相当于 es6中的findIndex</span><br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">var</span> arr = [<br>    &#123;<span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;test1&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1</span>&#125;,<br>    &#123;<span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;test2&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>&#125;,<br>    &#123;<span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;test3&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>&#125;<br>];<br><span class="hljs-keyword">var</span> result = arr.myFind(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">item</span>)</span>&#123;<br>    <span class="hljs-keyword">return</span> item.name == <span class="hljs-string">&#x27;test2&#x27;</span><br>&#125;)<br><span class="hljs-built_in">console</span>.log(result)<br></code></pre></div></td></tr></table></figure>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E5%89%8D%E7%AB%AF/">前端</a>
                    
                      <a class="hover-with-bg" href="/tags/nodejs/">nodejs</a>
                    
                      <a class="hover-with-bg" href="/tags/express/">express</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均为本人原创，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/07/29/NodeJS%E7%AC%94%E8%AE%B0/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">NodeJS笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/07/28/ajax%E7%AC%94%E8%AE%B0/">
                        <span class="hidden-mobile">ajax笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->

  <div class="col-lg-7 mx-auto nopadding-x-md">
    <div class="container custom post-custom mx-auto">
      <img src="/img/agoni.gif" srcset="/img/loading2.gif" lazyload class="rounded mx-auto d-block mt-5" style="width:150px; height:150px;"> <h5 style="text-align: center; margin-top: 1rem;">谢谢您的光临噢！！！</h5> 
    </div>
  </div>


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <span>张宝文</span> <i class="iconfont icon-love"></i> <span>郑佳丽</span>  
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('07/28/2021 19:00:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>

  <script>
    function typing(id, title){
        var typed = new Typed('#' + id, {
            strings: [
              '  ',
              title + "&nbsp;",
            ],
            cursorChar: "❤",
            typeSpeed: 150,
            loop: false,
        });
        typed.stop();
        $(document).ready(function () {
            $(".typed-cursor").addClass("h2");
            typed.start();
        });
    }

    
        typing("subtitle", document.getElementById('subtitle').title)
    

  </script>













  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/yinghua.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

</body>
</html>
